(function() {
	var util = require('common:widget/ui/libs/util.js').util,
        Dialog = require('common:widget/ui/dialog/dialog.js').Dialog,
        dialog = new Dialog();

    var pageUtil =  {
		head : null,
		current_page : 1,
        pid:1,
		page_count : 0,
		init : function() {
			var me = this,
                urlParam = util.getUrlParas();
            if(urlParam['pid']){
                me.pid = urlParam['pid'];
            }
			me.eventCenter();
			me.loadData();
            if(me.pid == 1){
                me.loadSummaryData();
            }
			$('.time_scope li').eq(0).addClass('selected');
		},
		loadData : function() {
			var me = this;
			$.getJSON('/user/usercreditassemble' , {
                "current_page" :me.current_page,
                'pid':me.pid
            },function (data) {
				if(data && data.error_no == 0 && data.data) {
					me.page_count = data.data.page_count;
					var list = data.data.credit_assemble;
					$('.table tbody').empty();
					for(var index in list) {
						var el = list[index];
						// <th>债权名称</th>
						var html = '<tr><td>'+el['credit_name']+'</td>';
						// <th>债权类型</th>
						var typeStr = '';
						if(el['type'] == 0) {
							typeStr = '抵押标';
						} else if(el['type'] == 1) {
							typeStr = '流转标';
						} else if(el['type'] == 2) {
							typeStr = '净值标';
						}
						html += '<td>'+ typeStr+'</td>';
						// <th>平台名称</th>
						html += '<td>'+ el['plat_name']+'</td>';
						// <th>我的转让金额</th>
						html += '<td>'+ mathUtil.formatAmount(el['amount'],4)+'</td>';
						// <th>转让年化收益率</th>
						html += '<td>' + mathUtil.toPercent(el['profit_years_percent']) +'%' +'</td>';
                        if(me.pid ==1){
                            // <th>真融宝风险备用金率</th>
                            html += '<td>' + mathUtil.toPercent(el['profit_backup_years_percent']) +'%' +'</td>';
                            // <th>真融宝转让年化收益率</th>
                            html += '<td>' + mathUtil.toPercent(el['weight_profit_years_percent']) +'%' +'</td>';
                        }
						// <th>借款周期</th>
						html += '<td>'+ el['loan_life']+'天</td>';
						html += '</tr>';
						$('.table tbody').append(html);
					}
					me.loadDataComplete();
				} else {
					if(data.error_no != 0) {
						dialog.show(data.error_message);
					}
				}
			})
		},
		loadSummaryData : function() {
			var me = this;
			$.getJSON('/plat/plat_fund_summary',{
                pid:me.pid
            },function (data) {
				if(data && data.error_no == 0 && data.data) {
					var usable_credit = parseInt(data.data.plat_credit_buy);
					$('#usable_credit .ob_title').text("￥" + mathUtil.toCredit(usable_credit));
					$('#today_credit .ob_title').text("￥" + mathUtil.toCredit(data.data.plat_total_buy));
					$('#credit_count .ob_title').text(data.data.credit_assemble_total);
					$('#plat_count .ob_title').text(data.data.credit_plat_total);
				} else {
					if(data.error_no != 0) {
						dialog.show(data.error_message);
					}
				}
			})
		},
		loadDataComplete:function() {
			var me = this;
			if(!me.pagination) {
				var Pagination = require('home:widget/ui/pagination/pagination.js');
				me.pagination = new Pagination($('#record'), me.page_count);	
			}
		},
		eventCenter : function() {
			var me = this;
 			$('#record').bind('changePage',function(event,data) {
 				me.current_page = data.goto_num;
 				me.loadData();
 			})
		}
	}
	pageUtil.init();
})();